﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

using Mvc.XForms.Utilities;

namespace Mvc.XForms.Controls {
	public static class Select1Extensions {
		public static string Select1<T>(this HtmlHelper html, string id, IEnumerable<T> items, Func<T, object> valueSelector, Func<T, object> textSelector) {
			return Select1(html, id, items, valueSelector, textSelector, null);
        }

		public static string Select1<T>(this HtmlHelper html, string id, IEnumerable<T> items, Func<T, object> valueSelector, Func<T, object> textSelector, object selectedValue) {
			var list = items.Select(t => new { value = valueSelector(t), text = textSelector(t) });

			return html.DropDownList(id, new SelectList(list, "value", "text", selectedValue));
		}
    }
}